<template>
  <div class="cartDetail">
    <div class="content">
      <van-checkbox-group v-model="checked" @change="groupChange">
        <div v-for="(item, index) in store.state.cartList" :key="index">
          <list-item
            :item="item"
            :handleChange="handleChange"
            :showCheckBox="true"
          ></list-item>
        </div>
      </van-checkbox-group>
    </div>
    <van-submit-bar
      :price="allPrice"
      button-text="结算"
      @submit="onSubmit"
      button-color="#ffc300"
      v-if="store.state.isDelete"
      class="submit-all"
    >
      <van-checkbox
        v-model="submitChecked"
        checked-color="#ffc300"
        @click="choseAll"
        >全选</van-checkbox
      >
    </van-submit-bar>
    <div class="buy" v-else>
      <div class="left">
        <van-checkbox
          v-model="submitChecked"
          checked-color="#ffc300"
          @click="choseAll"
          >全选</van-checkbox
        >
      </div>
      <div class="delete" @click="handleDelete">删除</div>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, onMounted, computed } from "vue";
import { useStore } from "vuex";
import ListItem from "@/components/ListItem.vue";
import { showFailToast } from "vant";
import { useRouter } from "vue-router";
export default {
  components: {
    ListItem,
  },
  props: ["changeShow"],
  setup(props) {
    let router = useRouter();
    const store = useStore();
    const handleChange = (value, detail) => {
      store.state.cartList.map((item) => {
        if (item.id === detail.name) {
          item.num = value;
        }
      });
    };
    const data = reactive({
      checked: [],
      submitChecked: true,
    });

    const init = () => {
      data.checked = store.state.cartList.map((item) => item.id);
    };
    const onSubmit = () => {
      if (data.checked.length) {
        store.commit("pay", updateData());
        router.push({
          path: "/createOrder",
          query: {
            list: data.checked,
          },
        });
      } else {
        showFailToast("请选择商品~");
      }
    };
    const choseAll = () => {
      if (data.checked.length !== store.state.cartList.length) {
        init();
      } else {
        data.checked = [];
      }
    };
    const groupChange = (result) => {
      console.log(result);
      if (result.length === store.state.cartList.length) {
        data.submitChecked = true;
      } else {
        data.submitChecked = false;
      }
      data.checked = result;
    };
    const allPrice = computed(() => {
      let countList = updateData();
      let sum = 0;
      countList.forEach((item) => {
        sum += item.num * item.price;
      });
      return sum * 100;
    });
    const updateData = (type) => {
      return store.state.cartList.filter((item) => {
        return type === "delete"
          ? !data.checked.includes(item.id)
          : data.checked.includes(item.id);
      });
    };
    const handleDelete = () => {
      if (data.checked.length) {
        store.commit("delete", updateData("delete"));
        data.checked = [];
        if (!store.state.cartList.length) {
          props.changeShow();
          store.commit("edit", "delete");
        }
      } else {
        showFailToast("请选择你要删除的商品~");
      }
    };
    onMounted(() => {
      init();
    });
    return {
      ...toRefs(data),
      store,
      handleChange,
      onSubmit,
      choseAll,
      groupChange,
      allPrice,
      handleDelete,
      updateData,
    };
  },
};
</script>

<style lang="scss" scoped>
.cartDetail {
  position: relative;
  font-size: 14px;
  flex: 1;
  overflow-y: auto;
  padding: 20px 20px 55px;
  // background-color: red;
  .submit-all {
    position: fixed;
    bottom: 48px;
  }
  .buy {
    position: fixed;
    bottom: 48px;
    right: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    width: 100%;
    height: 50px;
    background-color: #fff;
    border-radius: 10px;
    box-sizing: border-box;
    .left {
      display: flex;
      align-items: center;
    }
    .delete {
      color: #fff;
      background-color: #ffc300;
      border-radius: 20px;
      font-size: 14px;
      font-weight: 600;
      width: 110px;
      height: 40px;
      text-align: center;
      line-height: 40px;
    }
  }
  .content {
    padding: 10px;
    background-color: #fff;
    border-radius: 10px;
  }
}
</style>
